package com.own.stu.apm.agent.core.plugin;

import com.own.stu.apm.agent.core.plugin.loader.AgentClassLoader;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

@Slf4j
public class PluginResourcesResolver {

    /**
     * 获取插件目录(/plugins)下所有jar内的skywalking-plugin.def文件
     *
     * @return
     */
    public List<URL> getResources() {
        List<URL> cfgUrlPaths = new ArrayList<>();
        Enumeration<URL> resources = null;
        try {
            resources = AgentClassLoader.getDefault().getResources("plugins.def");
            while (resources.hasMoreElements()) {
                URL url = resources.nextElement();
                cfgUrlPaths.add(url);
                log.info("find plugin.def file: {}", url);
            }
        } catch (IOException e) {
            log.error("read source fail", e);
        }
        return cfgUrlPaths;
    }
}
